<?php
namespace app\massage\controller;
use app\AdminRest;
use app\BaseController;
use app\massage\model\Service;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Cap;
use app\shop\model\GoodsCate;
use app\shop\model\GoodsSh;
use app\shop\model\GoodsShList;
use think\App;
use app\shop\model\Goods as Model;
use think\Db;
use think\facade\View;


class Tcp extends BaseController
{


    protected $model;

    protected $goods_sh;

    protected $goods_sh_list;

    public function __construct(App $app) {

        parent::__construct($app);

//        $this->model = new Service();


    }



    /**
     * @author chenniang
     * @DataTime: 2021-09-29 16:25
     * @功能说明:远程获取信息
     */
    public function getInfo(){

        //获取param
        $input= $this->request->param();

        if(!empty($input['card_id'])&&!empty($input['time'])){

            $info = \think\facade\Db::name('shequshop_car_atv_speed_test')->where(['card_id'=>$input['card_id']])->find();

            $insert = [

                'card_id' => $input['card_id'],

                'time'    => date('Y-m-d H:i:s',time()),

                'uniacid' => 1,

                'time_str'=> time()
            ];

            if(empty($info)){

                \think\facade\Db::name('shequshop_car_atv_speed_test')->insert($insert);

            }else{

                \think\facade\Db::name('shequshop_car_atv_speed_test')->where(['id'=>$info['id']])->update($insert);
                //如果两次信号间隔时间小于30秒 不记录
                if(time()-$info['time_str']<30){

                    echo 2;exit;

                }

            }

        }else{

            echo true;exit;

        }

        $card_id = $input['card_id'];

        $dis = [

            'now_card_id' => $card_id
        ];

        $game = \think\facade\Db::name('shequshop_car_game')->where($dis)->find();

        //当前绑卡的比赛
        if(!empty($game)){

            $dis = [

                'record_id' => $game['id'],

                'end_time'  => 0
            ];
            //查看是开始还是结束
            $is_start = \think\facade\Db::name('shequshop_car_atv_speed')->where($dis)->find();

            $c_time = time()-$game['last_time'];
            //如果两个信号接受时间未超过40秒 不记录信息
            if($c_time<30){

                echo false;exit;
            }
            //开始
            if(empty($is_start)){

                $insert = [

                    'uniacid'    => $game['uniacid'],

                    'record_id'  => $game['id'],

                    'user_id'    => $game['user_id'],

                    'start_time' => $input['time'],

                ];

                \think\facade\Db::name('shequshop_car_atv_speed')->insert($insert);

            }else{

//                //如果两个信号接受时间未超过40秒 不记录信息
//                if($c_time<40){
//
//                    echo false;exit;
//                }

                $time = $input['time'] - $is_start['start_time'];
                //结束
                $update = [

                    'end_time'  => $input['time'],

                    'time'      => $time,

                ];

                \think\facade\Db::name('shequshop_car_atv_speed')->where(['id'=>$is_start['id']])->update($update);

                if($time<$game['best_time']||$game['best_time']==0){

                    $update_game = [

                        'best_time' => $time
                    ];

                }

                $total_time = $game['total_time']+$time;

                $game['have_num']++;

                $update_game['total_time'] = $total_time;

                $update_game['have_game']  = 1;

                $update_game['status']     = 2;

                $update_game['have_num']   = $game['have_num'];
                //修改最好成绩
                \think\facade\Db::name('shequshop_car_game')->where(['id'=>$game['id']])->update($update_game);
                //第二圈开始
                $insert = [

                    'uniacid'    => $game['uniacid'],

                    'record_id'  => $game['id'],

                    'user_id'    => $game['user_id'],

                    'start_time' => $input['time'],

                ];

                \think\facade\Db::name('shequshop_car_atv_speed')->insert($insert);

            }

            $update = [

                'last_time' => intval($input['time']/1000),

            ];

            if(empty($game['start_time'])){

                $update['start_time'] = intval($input['time']/1000);

                $update['day'] = date('Y-m-d',time());
            }
            //修改最后接受信息时间
            \think\facade\Db::name('shequshop_car_game')->where(['id'=>$game['id']])->update($update);

        }


        echo true;exit;


    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-28 14:53
     * @功能说明:
     */
    public function gameList(){

        $dis = [

            'uniacid'   => 1,

            'have_game' => 1

        ];

        $data = \think\facade\Db::name('shequshop_car_game')->where($dis)->field('best_time,start_time,user_id')->order('start_time desc,best_time')->limit(50)->select()->toArray();


        if(!empty($data)){

            foreach ($data as &$v){

               $v['user_name']  =  \think\facade\Db::name('massage_service_user_list')->where(['id'=>$v['user_id']])->value('nickName');

               $v['start_time'] = date('m-d H:i:s',$v['start_time']);

               $v['best_time']  = game_time($v['best_time']);

            }

        }

        $count = count($data);

        View::assign('data', $data);

        View::assign('count', $count);

        return View::fetch();

        echo json_encode($datas);exit;

    }



















}
